package com.usefullc.extinguisher.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.common.exception.BizException;
import com.usefullc.common.user.OnlineUser;
import com.usefullc.common.user.OnlineUserManager;
import com.usefullc.extinguisher.param.FireDeviceAuditParam;
import com.usefullc.extinguisher.service.ExtinguisherTestService;
import com.usefullc.extinguisher.service.dataTransfer.DataTransferService;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.extinguisher.domain.FireDevice;
import com.usefullc.extinguisher.param.FireDeviceParam;
import com.usefullc.extinguisher.vo.FireDeviceVo;
import com.usefullc.extinguisher.query.FireDeviceQuery;
import com.usefullc.extinguisher.service.FireDeviceService;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 消防设备控制类
 * @author: 星空
 * @date: 2022-09-08
 */
@RestController
@RequestMapping("/extinguisher/fireDevice/")
@Api( tags="消防设备")
public class FireDeviceController extends BaseController {

    @Autowired
    private FireDeviceService fireDeviceService;

    @Autowired
    private DataTransferService dataTransferService;

    @Autowired
    private ExtinguisherTestService extinguisherTestService;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "消防设备列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(@UsefulValid(query = true) FireDeviceQuery query) {
        query.setOrderByColumn("code");
        query.setAscOrDesc("asc");
        Pager<FireDeviceVo> page = fireDeviceService.findVoByPage(query);
        return WebResultBuilder.successResult(page);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "消防设备所有列表", response = WebResult.class)
    @GetMapping(path = "listAll", produces = "application/json")
    public WebResult listAll(@UsefulValid(query = true) FireDeviceQuery query) {
        List<FireDevice> dataList = fireDeviceService.findAll(query);
        return WebResultBuilder.successResult(dataList);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "消防设备详情",response = WebResult.class)
    @GetMapping(path = "detail",produces = "application/json")
    public WebResult detail(@ApiParam(value = "示例ID") @Valid @RequestParam Long id) {
        FireDevice fireDevice = fireDeviceService.findById(id);
        return WebResultBuilder.successResult(fireDevice);
    }



    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "创建消防设备", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    @UsefulValid
    public WebResult save(@UsefulValid(save=true) @RequestBody FireDeviceParam param) {
        FireDevice fireDevice = BeanUtils.beanCopy(param, FireDevice.class);
        String code = fireDevice.getCode();
        code = code.replaceAll("–", "-");
        fireDevice.setCode(code);
        return successResult(fireDeviceService.save(fireDevice));
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改消防设备", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@UsefulValid(update=true) @RequestBody FireDeviceParam param) {
        FireDevice oldBean = fireDeviceService.findById(param.getId());
        BeanUtils.beanCopy(param, oldBean);
        return successResult(fireDeviceService.update(oldBean));
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除消防设备",response = WebResult.class)
    public WebResult del(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        fireDeviceService.deletePhysByIds(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "逻辑删除消防设备",response = WebResult.class)
    public WebResult delByLogic(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        fireDeviceService.deletePhysByIds(idParam.getIds());
        return WebResultBuilder.successResult();
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "审核消防设备", response = WebResult.class)
    @PostMapping(path = "/audit", produces = "application/json")
    public WebResult audit(@RequestBody FireDeviceAuditParam param) {
        if(param.getId() == null && StringUtils.isEmpty(param.getCode())){
            throw new BizException("ID和编码不能同时为空");
        }
        if(param.getId() == null){
            FireDevice fireDevice = fireDeviceService.findRawByCode(param.getCode());
            param.setId(fireDevice.getId());
        }
        fireDeviceService.audit(param.getId(),param.getStatus(),param);
        return successResult();
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "根据编码获取消防设备详情",response = WebResult.class)
    @GetMapping(path = "findByCode",produces = "application/json")
    public WebResult findByCode(@ApiParam(value = "编码") @Valid @RequestParam String code) {
        FireDeviceVo fireDeviceVo = fireDeviceService.findByCode(code);
        return WebResultBuilder.successResult(fireDeviceVo);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "测试", response = WebResult.class)
    @PostMapping(path = "/test", produces = "application/json")
    public WebResult test() {
//        dataTransferService.saveDeviceDataDict();
//        dataTransferService.deviceFromJsonToDB();
//        dataTransferService.userFromJosnToDB();


//        extinguisherTestService.test1();

        return successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "消防设备二维码打印",response = WebResult.class)
    @GetMapping(path = "qrCodePrint",produces = "application/json")
    public WebResult qrCodePrint(@UsefulValid(query = true) FireDeviceQuery query) {
        String printHtml = fireDeviceService.getPrintHtml(query);
        return WebResultBuilder.successResult(printHtml);
    }

    //导入，导出

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "消防设备导出",response = WebResult.class)
    @GetMapping(path = "exportExcel",produces = "application/json")
    public void exportExcel(@UsefulValid(query = true) FireDeviceQuery query, HttpServletResponse response) {
        fireDeviceService.export(query,response);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "消防设备导入",response = WebResult.class)
    @PostMapping(path = "importExcel", headers = "content-type=multipart/form-data", produces = "application/json")
    public WebResult importExcel(MultipartFile file, String biz) {
        try{
            fireDeviceService.readXls(file.getInputStream());
        }catch (Exception e){
            throw new BizException(e);
        }
        return WebResultBuilder.successResult();
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改生产日期",response = WebResult.class)
    @PostMapping(path = "updateBuildTime", produces = "application/json")
    public WebResult updateBuildTime(@UsefulValid(query = true) @RequestBody FireDeviceQuery query) {
        if(query.getGmtBuildUpdate() == null){
            throw new BizException("生产日期不能为空");
        }
        fireDeviceService.updateBuildTime(query);
        return WebResultBuilder.successResult();
    }




}
